<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<!--标准mui.css-->
		<link rel="stylesheet" href="css/mui.css">
		<!--App自定义的css-->

		<style>
			
		</style>
	</head>

	<body>

		<img src="" class="haedphoto"  style="width: 200px;display: block;margin: 0 auto;"/>
		<h2 class="username" style="font-size: 1em;text-align: center;width: 100%;"></h2>
			
		<p class="codep"></p>


	
	</body>
	<script src="../js/mui.min.js"></script>
	<script>
		mui.init({
			swipeBack:true //启用右滑关闭功能
		});
	</script>



<script  type="text/javascript" Author="Cchua" >

window.addEventListener('load',function(){
    var ua = navigator.userAgent.toLowerCase();
    // alert(localStorage.openId);
    if (ua.match(/MicroMessenger/i) != "micromessenger") {
		console.log("您使用的是非微信端的浏览器")
	}else{
//		localStorage.removeItem('userToken')
//		alert(window.location.href);
console.log("localStorage.userToken 是:"+localStorage.userToken )
console.log(localStorage.userToken == undefined)
		if(localStorage.userToken == undefined){
			WechatNewLogin();
		}
		else{
			 WechatLogin();
		}
	}

});

// window.onbeforeunload = function(){
// 	WechatNewLogin();
// }

function WechatLogin(){
	document.querySelector(".codep").innerHTML = localStorage.userToken;
	mui.ajax({
		"url":URL_all+"api/wechat/openidata",
		"dataType":"json",
		"type":"post",
		"timeout":100000000000,
		"headers":"{'Content-Type':'application/json','userToken':"+localStorage.userToken+"}",
		"success":function(data){
			console.log("openidata为:"+JSON.stringify(data));
			
				localStorage.userToken=data.dataResultObj
				var sta = data.status;
		if (sta!=200) {
			// location.reload()
			
			WechatNewLogin();
		}else{

			mui(".username")[0].innerText=data.dataResultList.nickName;
		}
		},
		"error":function(xhr,type,errorThrown){
			//异常处理；
			console.log(type);
		}
	})
//	mui.ajax('http://hsmytest.vrdete.com//api/wechat/openidata',{
//		dataType:'json',//服务器返回json格式数据
//		type:'post',//HTTP请求类型
//		timeout:10000000000,
//		headers:{'Content-Type':'application/json','userToken':localStorage.userToken},
//		success:function(data){
//			console.log(JSON.stringify(data));
//		var integral_head = document.getElementsByClassName('integral_head')[0];
//		var sta = data.status;
//		if (sta!=200) {
//			WechatNewLogin();
//		}
//		else{
////						if(window.location.pathname=="/mp/myself.html"){
//					document.querySelector(".haedphoto")[0].src=data.dataResultObj.headimgurl;
//					document.querySelector(".username")[0].innerText=data.dataResultObj.nickName;
////						}
////						if(window.location.pathname=="/mp/integral.html"){
//					integral_head.src=data.dataResultObj.headimgurl;
////						}
//
//			}
//	
//		},
//		error:function(xhr,type,errorThrown){
//			//异常处理；
//			console.log(type);
//		}
//	});		

}
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

function WechatNewLogin() {
var appid;
var myurl=GetQueryString("code");  
console.log("myurl是:"+myurl);
if(myurl !=null && myurl.toString().length>1){
	var usercode= GetQueryString("code");
	
//	mui.ajax({
//		"url":"https://api.weixin.qq.com/sns/oauth2/access_token?appid="+localStorage.appId +"&secret=SECRET&code="+usercode+"&grant_type=authorization_code",
//		"dataType":"json",
//		"type":"post",
//		"data":{'code':usercode},
//		"timeout":10000,
//		"headers":"{'Content-Type':'application/json'}",
//		"success":function(data){
//			console.log("postwcode为:"+JSON.stringify(data));
//		},
//		"error":function(xhr,type,errorThrown){
//			//异常处理；
//			console.log(type);
//		}
//	})
	
   	console.log("已经获取到Code："+GetQueryString("code"));
   	console.log("即将调用后台获取用户数据");
   // alert(usercode);
	mui.ajax({
		"url":URL_all+"api/wechat/postwcode",
		"dataType":"json",
		"type":"post",
		"data":"{'code':'"+usercode+"'}",
		"timeout":10000,
		headers:{'Content-Type':'application/json'},	
		/*"headers":"{'Content-Type':'application/json'}",*/	
		"success":function(data){
			console.log("postwcode为:"+JSON.stringify(data));
				// document.querySelector(".username").innerText = data.dataResultObj.nickName;
			mui(".haedphoto")[0].src = data.dataResultObj.headimgurl;
                 mui(".username")[0].innerText = data.dataResultObj.nickName;
				localStorage.userToken=data.dataResultObj
		},
		"error":function(xhr,type,errorThrown){
			//异常处理；
			console.log(type);
		}
	})
//	mui.ajax('http://hsmytest.vrdete.com/api/wechat/postwcode',{
//		data:"{'code':'"+usercode+"'}",
//			dataType:'json',//服务器返回json格式数据
//			type:'post',//HTTP请求类型
//			timeout:10000,//超时时间设置为10秒；
//			headers:{'Content-Type':'application/json'},	              
//			success:function(data){
////				alert(11);
//				console.log(JSON.stringify(data));
//				localStorage.userToken = data.userToken;
////				console.log(localStorage.userToken);
////				console.log("openId:"+JSON.stringify(data.dataResultObj.openId));
////				console.log(data.dataResultList[0].id);
////				localStorage.openId=data.dataResultObj.openId;
////				localStorage.userId=data.dataResultList[0].id;
////              if(window.location.pathname=="/mp/myself.html") {
//                  mui(".haedphoto")[0].src = data.dataResultObj.headimgurl;
//                  mui(".username")[0].innerText = data.dataResultObj.nickName;
////              }
//
//
///*localStorage 后面接的是Key值  让后赋值*/
//
//			},
//			error:function(xhr,type,errorThrown){
//				//异常处理；
//				// alert(type);
//				console.log(type);
//			}
//		});		
}
else{
//  alert(2);

//  	console.log("未获取到Code")
//		console.log("WechatLogin:Go")
//		console.log(location.pathname);
		var tourl = window.location.href;
		console.log("tourl为:"+tourl);
		mui.ajax({
			"url":URL_all+"api/wechat/authorize",
			"dataType":"json",
			"type":"post",
			"timeout":10000,
		headers:{'Content-Type':'application/json'},	
			"success":function(data){
				console.log("authorize为:"+JSON.stringify(data)); //data.dataResultObj.AppId wx253b97a570d99ccc
//				console.log(data.dataResultObj.AppId ) //wx253b97a570d99ccc
				appid = data.dataResultObj.AppId;
				localStorage.appId =   data.dataResultObj.AppId;
				window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+tourl+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
			},
			"error":function(xhr,type,errorThrown){
				//异常处理；
				console.log(type);
			}
		})
//		mui.ajax('http://hsmytest.vrdete.com/api/wechat/authorize',{
//			data:{},
//			dataType:'json',//服务器返回json格式数据
//			type:'post',//HTTP请求类型
//			timeout:10000,//超时时间设置为10秒；
//			headers:{'Content-Type':'application/json'},	              
//			success:function(data){
////				appid:wx253b97a570d99ccc
//				console.log("in mui.ajax type post");
////				alert("你已取得Appid:"+data.dataResultObj.AppId);
//
//				appid = data.dataResultObj.AppId;
//
// 				window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+tourl+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
//
//				console.log(data);
//				
//			},
//			error:function(xhr,type,errorThrown){
//				//异常处理；
//				console.log(type);
//			}
//
//		});	
}
}

</script>



</html>